package com.yq.light.user.code.controller;

import com.yq.light.commons.code.entity.CommonResult;
import com.yq.light.commons.code.entity.User;
import com.yq.light.user.code.service.UserService;
import com.yq.light.user.code.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/registerAccount")
    @ResponseBody
    public CommonResult registerAccount(@RequestBody User user){
        log.info("registerAccount 参数 user:"+user);
        int result = userService.registerAccount(user);
        log.info("registerAccount result:"+result);
        if(result > 0){
            return new CommonResult(200,"注册成功",result);
        }
        return new CommonResult(200,"注册失败",result);
    }

    /**
     *
     * @param user userAccount,userPwd
     * @return CommonResult
     */
    @RequestMapping("/loginNoToken")
    @ResponseBody
    public CommonResult loginNoToken(@RequestBody User user){
        log.info("controller --loginNoToken parameter--user:"+user);
        String result = null;
        try{
        result = userService.loginNoToken(user.getUserAccount(),user.getUserPwd());
            if(result!=null){
                return new CommonResult(200,"登录成功",result);
            }
        }catch (Exception e){
            log.info("exception on UserController method:loginNoToken --"+e.toString());
        }
        return new CommonResult(200,"登录失败","Not find return message!");
    }

    /**
     * 用户登录成功之后请求用户信息
     * @param token
     * @return
     */
    @RequestMapping("/info")
    @ResponseBody
    public CommonResult userInfo(@RequestParam("token") String token){
        log.info("userAccount --"+token);
        User userInfo = userService.userInfo(token);
        log.info("userInfo is --"+userInfo.toString());
        try{
            if(userInfo!=null)
                return new CommonResult(200,"获取成功",userInfo);
        }catch (Exception e){
            log.info("exception on UserController method:userInfo --"+e.toString());
        }
        return new CommonResult(200,"获取失败","no user info!");
    }

    @RequestMapping("/emailPost")
    @ResponseBody
    public CommonResult emailPost(@RequestParam("who") String who){
        log.info(who);
        userService.sendEmail(who,"测试邮件！","测试邮件内容--2352399091--");
        return new CommonResult<>(200,"发送",1);
    }
}
